#include "messageboxutil.h"

#include <QAbstractButton>

MessageBoxUtil::MessageBoxUtil(QObject *parent) : QObject(parent)
{
}
int MessageBoxUtil::hintMessage(QWidget *parent, MessageBoxUtil::MessageBoxType type, const QString &title, const QString &text)
{

    // 创建一个question弹出对话框，添加两个按钮：Yes和No
    QMessageBox *msgBox = new QMessageBox(QMessageBox::Question, title, text, QMessageBox::Ok, parent);

    // 设置消息框图标
    switch (type) {
    case Question:
        msgBox->setIcon(QMessageBox::Question);
        break;
    case Information:
        msgBox->setIcon(QMessageBox::Information);
        break;
    case Warning:
        msgBox->setIcon(QMessageBox::Warning);
        break;
    case Critical:
        msgBox->setIcon(QMessageBox::Critical);
        break;
    }

    // 将原本显示“Yes”的按钮改为显示“是”
    msgBox->button(QMessageBox::Ok)->setText(tr("确定"));

    // 弹出对话框
    return msgBox->exec();
}

int MessageBoxUtil::showMessage(QWidget *parent, MessageBoxUtil::MessageBoxType type, const QString &title, const QString &text)
{

    // 创建一个question弹出对话框，添加两个按钮：Yes和No
    QMessageBox *msgBox = new QMessageBox(QMessageBox::Question, title, text, QMessageBox::Yes | QMessageBox::No, parent);

    // 设置消息框图标
    switch (type) {
    case Question:
        msgBox->setIcon(QMessageBox::Question);
        break;
    case Information:
        msgBox->setIcon(QMessageBox::Information);
        break;
    case Warning:
        msgBox->setIcon(QMessageBox::Warning);
        break;
    case Critical:
        msgBox->setIcon(QMessageBox::Critical);
        break;
    }

    // 将原本显示“Yes”的按钮改为显示“是”
    msgBox->button(QMessageBox::Yes)->setText(tr("是"));

    // 将原本显示“No”的按钮改为显示“否”
    msgBox->button(QMessageBox::No)->setText(tr("否"));

    // 弹出对话框
    return msgBox->exec();
}

//int MessageBoxUtil::showMessage(QWidget *parent, MessageBoxUtil::MessageBoxType type, const QString &title, const QString &text, MessageBoxUtil::ButtonTypes buttons)
//{
//    QMessageBox msgBox(parent);
//    msgBox.setWindowTitle(title);
//    msgBox.setText(text);

//    // 设置消息框图标
//    switch (type) {
//    case Question:
//        msgBox.setIcon(QMessageBox::Question);
//        break;
//    case Information:
//        msgBox.setIcon(QMessageBox::Information);
//        break;
//    case Warning:
//        msgBox.setIcon(QMessageBox::Warning);
//        break;
//    case Critical:
//        msgBox.setIcon(QMessageBox::Critical);
//        break;
//    }

//    // 设置按钮
//    QMessageBox::StandardButtons standardButtons;
//    for (int i = Ok; i <= Ignore; i <<= 1) {
//        if (buttons & i) {
//            ButtonType button = static_cast<ButtonType>(i);
//            QMessageBox::StandardButton standardButton = convertToStandardButton(button);
//            standardButtons |= standardButton;
//            msgBox.addButton(standardButton);
//            msgBox.setButtonText(standardButton, getButtonText(button));
//        }
//    }
//    msgBox.setStandardButtons(standardButtons);

//    // 执行消息框并返回结果
//    return msgBox.exec();
//}

QMessageBox::StandardButton MessageBoxUtil::convertToStandardButton(MessageBoxUtil::ButtonType button)
{
    switch (button) {
    case Ok: return QMessageBox::Ok;
    case Cancel: return QMessageBox::Cancel;
    case Yes: return QMessageBox::Yes;
    case No: return QMessageBox::No;
    case Abort: return QMessageBox::Abort;
    case Retry: return QMessageBox::Retry;
    case Ignore: return QMessageBox::Ignore;
    default: return QMessageBox::NoButton;
    }
}

QString MessageBoxUtil::getButtonText(MessageBoxUtil::ButtonType button)
{
    switch (button) {
    case Ok: return "确定";
    case Cancel: return "取消";
    case Yes: return "是";
    case No: return "否";
    case Abort: return "中止";
    case Retry: return "重试";
    case Ignore: return "忽略";
    default: return QString();
    }
}
